#include "GameControl.h"
#include <graphics.h>
#include <conio.h>
#include <tchar.h>
#include <Windows.h>

#define block_width 20
#define x_origin 0
#define y_origin 0
#define screenblock_x 600
#define screenblock_y 480

GameControl::GameControl() : food(snake.getBody()) {}

void GameControl::drawOtherBlock(int body_x, int body_y, int width, int height, const char* pic) {
    IMAGE img;
    int len = MultiByteToWideChar(CP_ACP, 0, pic, -1, NULL, 0);
    wchar_t* wPic = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, pic, -1, wPic, len);
    loadimage(&img, wPic, width, height);
    delete[] wPic;
    putimage(body_x, body_y, &img);
}

bool GameControl::Collision() {
    std::vector<int> head = snake.getHead();
    if (head[0] <= 3 || head[1] <= 3 || head[0] >= (screenblock_x - 3 * block_width) / block_width - 1 || head[1] >= (screenblock_y - 3 * block_width) / block_width - 1) {
        drawOtherBlock(180, 430, 240, 60, "LOSE.png");
        return false;
    }
    std::vector<std::vector<int>> body = snake.getBody();
    for (int i = 1; i < body.size(); i++) {
        if (head[0] == body[i][0] && head[1] == body[i][1]) {
            drawOtherBlock(180, 430, 240, 60, "LOSE.png");
            return false;
        }
    }
    return true;
}

bool GameControl::eatFood() {
    std::vector<int> head = snake.getHead();
    std::vector<int> foodPos = food.getPosition();
    return head[0] == foodPos[0] && head[1] == foodPos[1];
}

void GameControl::startGame() {
    initgraph(screenblock_x, screenblock_y);
    setbkcolor(RGB(158, 211, 224));
    cleardevice();
    drawOtherBlock(175, 320, 250, 87, "press_r_enter_game.png");
    drawOtherBlock(0, 20, 600, 276, "title.png");
    do {
        FlushBatchDraw();
        if (_kbhit()) {
            char key = _getch();
            if (key == 'r' || key == 'R') {
                closegraph();
                break;
            }
        }
        Sleep(100);
    } while (true);
}

void GameControl::playGame() {
    initgraph(screenblock_x, screenblock_y);
    setbkcolor(RGB(239, 255, 150));
    cleardevice();
    setcolor(RGB(255, 130, 97));
    rectangle(3 * block_width, 3 * block_width, screenblock_x - 3 * block_width, screenblock_y - 3 * block_width);
    drawOtherBlock(110, 0, 290, 60, "hint.png");
    score.display();
    food.draw();
    snake.draw();

    bool run = true;
    while (run) {
        if (_kbhit()) {
            char input = _getch();
            if (input == 'q') {
                break;
            }
            snake.move(input);
        }
        else {
            snake.autoMove();
        }
        if (eatFood()) {
            score.increase();
            food.regenerate(snake.getBody());
            if (score.getScore() != 10) {
                food.draw();
            }
        }
        else {
            snake.removeTail();
        }
        run = Collision();
        Sleep(200);
        if (score.getScore() == 10) {
            run = false;
        }
    }
    _getch();
    closegraph();
}